package net.jxiny.oschina;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;

import net.jxiny.oschina.net.OuathClient;
import net.jxiny.oschina.util.ACache;
import net.jxiny.oschina.util.CallBackForUser;
import net.jxiny.oschina.util.LoginAccessUtil;
import net.jxiny.oschina.view.MainActivity;

import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.ButterKnife;

//登录
public class LoginActivity extends AppCompatActivity implements View.OnClickListener, CallBackForUser {

    @BindView(R.id.et_login_username)
    TextView username;
    @BindView(R.id.et_login_password)
    TextView password;


    private EditText et_login_username,et_login_password;
    private TextView tv_login_forget_password;
    private Button bt_login,bt_register;
    private LinearLayout login_pull;
    private ImageButton navigation_back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //初始化
        ButterKnife.bind(this);

        //绑定id
        et_login_username = findViewById(R.id.et_login_username);
        et_login_password = findViewById(R.id.et_login_password);
        tv_login_forget_password = findViewById(R.id.tv_login_forget_password);
        bt_login = findViewById(R.id.bt_login);
        bt_register = findViewById(R.id.bt_register);
        login_pull = findViewById(R.id.login_pull);
        navigation_back =findViewById(R.id.navigation_back);

        //监听事件
        et_login_username.setOnClickListener(this);
        et_login_password.setOnClickListener(this);
        tv_login_forget_password .setOnClickListener(this);
        bt_login .setOnClickListener(this);
        bt_register .setOnClickListener(this);
        login_pull.setOnClickListener(this);
        navigation_back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_login:
                loginThree();
                break;
            case R.id.navigation_back:
                finish();
                break;
        }
    }

    private void loginThree() {
        //定义OauthClient的对象 （认证与授权）
        OuathClient client  = new OuathClient();
        // 表示客户端ID，必选项
        client.setClientId("gEZlPuqqtHLyrbYvZ2ji");
        //应用私钥
        client.setClientSecret("D7778c5zRHaCPlw7k0Bg1sa66IiBW5ac");
        //回调地址
        client.setRedirectUrl("http://www.baidu.com");
        //获取文本转为字符串给username和password
        client.setUsername("18851861082");
        client.setPassword("125210");
        LoginAccessUtil.login(this,client);
    }

    @Override
    public void getUserMsg(String userJson) {
        ACache.get(this).put("user",userJson);
        ACache.get(this).put("isLogin",true);
        ACache.get(this).put("token", JSON.parseObject(userJson).getJSONObject("token").getString(
                        "accessToken"));
//        Toast.makeText(getApplicationContext(),JSON.parseObject(userJson).getJSONObject("token").getString(
//                "accessToken") , Toast.LENGTH_SHORT).show();
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }
}
